package com.demo.common.utils;

import com.demo.common.constants.ResponseCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JsonResult<T> {


    private int code;
    private String msg;
    private T data;


    public static JsonResult build() {
        return new JsonResult();
    }

    public static JsonResult build(int code) {
        return new JsonResult().code(code);
    }

    public static JsonResult build(int code, String msg) {
        return new JsonResult<String>().code(code).msg(msg);
    }

    public static <T> JsonResult<T> build(int code, T data) {
        return new JsonResult<T>().code(code).data(data);
    }

    public static <T> JsonResult<T> build(int code, String msg, T data) {
        return new JsonResult<T>().code(code).msg(msg).data(data);
    }

    public JsonResult<T> code(int code) {
        this.code = code;
        return this;
    }

    public JsonResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public JsonResult<T> data(T data) {
        this.data = data;
        return this;
    }


    public static JsonResult ok() {
        return build(ResponseCode.SUCCESS);
    }

    public static JsonResult ok(String msg) {
        return build(ResponseCode.SUCCESS, msg);
    }

    public static <T> JsonResult<T> ok(T data) {
        return build(ResponseCode.SUCCESS, data);
    }

    public static JsonResult err() {
        return build(ResponseCode.EXCEPTION);
    }

    public static JsonResult err(String msg) {
        return build(ResponseCode.EXCEPTION, msg);
    }

    public static JsonResult err(int code, String msg) {
        return build(code, msg);
    }

    @Override
    public String toString() {
        return JsonUtil.to(this);
    }
}